var intervalo = setInterval ("decrement()", 1000);
var timerdiv = document.getElementById("timeremaining");
var time = document.getElementById("parrafo").innerHTML;
var xmlhttp;

function decrement() {
	if(time-1 < 0){
		//reinicia timer, actualiza el grafico y busca la imagen
		////////time = document.getElementById("parrafo2").innerHTML;
		////////timerdiv.innerHTML = "<p id='parrafo'>" + timer(time*1000) + "</p>";
		window.parent.graphics.location.reload();
		terminoRound();
		        timerdiv.innerHTML = "<p id='parrafo'>" + timer(time*1000) + "</p>";
		
		//enable form
		$("#vote").button( "option", "disabled", false );
		document.getElementById("answerA").disabled = false; 
		document.getElementById("answerB").disabled = false; 
		
	}
	else {
		timerdiv.innerHTML = "<p id='parrafo'>"+ timer((time-1)*1000) +"</p>";
		time = time-1;
	}
}

function terminoRound()
{
	xmlhttp=new XMLHttpRequest();
	if (xmlhttp==null){
		alert ("An error occurred. Please update your browser.");
		return;
	}

	var qid = document.getElementById("qid").innerHTML;
	var uid = document.getElementById("usuarioid").innerHTML;
	
	var extracto = document.URL;
	extracto = extracto.replace("history", "lastRoundInfo");
	var url = extracto + "&qid=" + qid + "&uid=" + uid + "&image="+Math.random();

	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged()
{
	if (xmlhttp.readyState==4){
		var answer = xmlhttp.responseXML;
		var imagen = answer.getElementsByTagName("image")[0].childNodes[0].nodeValue;
		var result = answer.getElementsByTagName("answer")[0].childNodes[0].nodeValue;
		var rankin = answer.getElementsByTagName("ranking")[0].childNodes[0].nodeValue;
		var points = answer.getElementsByTagName("points")[0].childNodes[0].nodeValue;
		var tiempo = answer.getElementsByTagName("time")[0].childNodes[0].nodeValue;
		var room = answer.getElementsByTagName("roomtime")[0].childNodes[0].nodeValue;
		var urlnueva = document.URL;
		urlnueva = urlnueva.substring(0,urlnueva.indexOf("/servlets/"));
		urlnueva = urlnueva + "/resources/" + imagen;
		urlnueva = "<img src='" + urlnueva + "'/>"

		document.getElementById("lastanswer").innerHTML = urlnueva + "<br/>" + result;
		document.getElementById("rankpos").innerHTML = rankin;
		document.getElementById("points").innerHTML = points;
		time = tiempo;
		var progressValue = new Number(rankin);
		$("#progressbar").progressbar( "value" , progressValue.valueOf());
		if(room <=0){
			location.reload(true);
		}
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function two(x) {
	return ((x>9)?"":"0")+x
}

function timer(ms) {
	var sec = Math.floor(ms/1000)
	if(sec<0){
		return "";
	}
	var min = Math.floor(sec/60)
	sec = sec % 60
	t = two(sec)
	
	var hr = Math.floor(min/60)
	min = min % 60
	t = two(min) + ":" + t
	
	var day = Math.floor(hr/24)
	hr = hr % 24
	t = two(hr) + ":" + t
	if(day!="0")
		t = day + " day" + (day>1? "s ":" ") + t
	
	return t
}
